package com.example.threaddemo.util;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author Boolean
 * @Date 2021/3/15 14:25
 * @Version 1.0
 *
 * 继承Thread类创建线程类
 * 参考地址： https://blog.csdn.net/qq_29229567/article/details/80561813
 */
public class ThreadDemo extends Thread {

    private static int  Q = 0;
    private static int  Z = 0;

    private String name;

    public ThreadDemo(String name) {
        this.name=name;
    }

  //  @Override
   /* public void run() {
        // super.run();

        try {
            Long time = SM4ECB2D(Q,Z);
            System.out.println("-----------总时间---------"+time);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

    /**
     * 线程池管理线程
     * 循环创建线程
     */
    public static void  execTopJob( int xt ) {
        Long time = System.currentTimeMillis();
        ExecutorService fixedThreadPool= Executors.newFixedThreadPool(xt);
        for(int i=0;i<xt;i++){
            //Thread  t =new ThreadDemo("t"+i);
            //fixedThreadPool.execute(t);
            fixedThreadPool.execute(new ThreadDemo("t"+i){
                public void run() {
                    // super.run();

                    try {
                        Long time = SM4ECB2D(Q,Z);
                        System.out.println("-----------总时间---------"+time);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        fixedThreadPool.shutdown();
        while(true){
            if(fixedThreadPool.isTerminated()){
                System.out.println("-----------fixedThreadPool---------"+(System.currentTimeMillis() - time));
                break;
            }
        }
    }

    public static void main(String[] args) {
        Q = 2;
        Z = 3;
        execTopJob(3);
    }

    /**
     * 可提供给其他类调用的方法
     * @param q
     * @param z
     * @param xt
     */
    public static void getVal( int q,int z,int xt )  {
        Q = q;
        Z = z;
        execTopJob(xt);
    }

    /**
     * 执行方法
     * @param q
     * @param z
     * @return
     */
    public static Long SM4ECB2D(int q,int z){
        long beffor = System.currentTimeMillis();

        //执行方法

        long end = System.currentTimeMillis();
        long time =  end-beffor;

        return time;
    }
}
